package com.zzt.zt_trasact.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

/**
 * @author: zeting
 * @date: 2022/8/16
 */
public class AppChannelUtil {

    private Context mContext;
    private String channel;
    private static volatile AppChannelUtil instance;

    public AppChannelUtil() {
    }

    public static AppChannelUtil getInstance() {
        if (instance == null) {
            synchronized (AppChannelUtil.class) {
                if (instance == null) {
                    instance = new AppChannelUtil();
                }
            }
        }
        return instance;
    }

    public AppChannelUtil initContext(Context con) {
        mContext = con;
        return this;
    }

    public String getChannelStr() {
        if (!TextUtils.isEmpty(channel)) {
            return channel;
        }
        if (mContext != null) {
            try {
                PackageManager packageManager = mContext.getPackageManager();
                if (packageManager != null) {
                    ApplicationInfo applicationInfo = packageManager.getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA);
                    if (applicationInfo != null) {
                        if (applicationInfo.metaData != null) {
                            String channel = applicationInfo.metaData.get("CHANNEL") + "";
                            Log.i("Channel", "当前的渠道为:" + channel);
                            return channel;
                        }
                    }
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
                Log.e("Channel", e.getMessage() + "");
            }
        }
        return "debug";
    }

}
